package com.process.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.process.controller.util.Account;
import com.process.controller.util.AccountDTO;
import com.process.controller.util.Code;
import com.process.controller.util.R;
import com.process.dao.pojoDao.AdminDao;
import com.process.dao.pojoDao.CompanyDao;
import com.process.dao.pojoDao.TeacherDao;
import com.process.domain.outer.OutTeacher;
import com.process.domain.publi.Admin;
import com.process.domain.publi.Company;
import com.process.domain.publi.Student;
import com.process.domain.publi.Teacher;
import com.process.filter.JwtUtils;
import com.process.service.ExcelServ.StudentService;
import com.process.service.out.OutTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description:
 * @ClassName: Login
 * @Author: 殷凡卓
 * @Date: 2023/4/14 15:00
 */
@RestController
@Slf4j
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private OutTeacherService outTeacherService;
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private CompanyDao companyDao;

    @PostMapping
    public R Login(@RequestBody Account acc, ServletRequest servletRequest) {
        String accountUsername = acc.getAccountUsername();
        String accountPassword = acc.getAccountPassword();
        Integer accountIdentity = Integer.parseInt(acc.getAccountIdentity());



        // 创建 Calendar 对象并设置为当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        // 在 Calendar 对象中加上 1小时
        calendar.add(Calendar.HOUR, 1);
        // 获取加上 2 小时后的时间
        Date date = calendar.getTime();
        log.info(String.valueOf(date));

        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setKeepDate(date);
        accountDTO.setAccountIdentity(acc.getAccountIdentity());
        accountDTO.setAccountUsername(acc.getAccountUsername());


        if (accountIdentity == 1) {
            //1.1 学生username 是否有该账户
            LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Student::getStuPhone, accountUsername);
            Student student = studentService.getOne(queryWrapper);
            //1.2如果没有查询到则返回登录失败结果
            if (student == null) {
                return new R(Code.LOGIN_ERR, null, "没有该学生账户");
            }
            //1.3.密码比对，如果不一致则返回登录失败结果
            if (!student.getStuPassword().equals(accountPassword)
            ) {
                return new R(Code.LOGIN_ERR, null, "学生登陆密码错误");
            } else {
                //1.4登陆成功
                HttpServletRequest request = (HttpServletRequest) servletRequest;
                String username = (String) request.getAttribute("username");
                String identity = (String) request.getAttribute("identity");
                log.info("学生登陆成功" + username);
                String sign = jwtUtils.sign(acc);
                accountDTO.setName("欢迎学生："+student.getStuName());
                return new R(Code.LOGIN_OK, accountDTO, sign);
            }
        } else if (accountIdentity == 2) {
            //2.1校内
            LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teacher::getTeacherPhone, accountUsername);
            Teacher teacher = teacherDao.selectOne(queryWrapper);
            //2.2如果没有查询到则返回登录失败结果
            if (teacher == null) {
                return new R(Code.LOGIN_ERR, null, "没有该教师账户");
            }
            //2.3.密码比对，如果不一致则返回登录失败结果
            if (!teacher.getTeacherPassword().equals(accountPassword)
            ) {
                return new R(Code.LOGIN_ERR, null, "教师登录密码错误");
            } else {
                //1.4登陆成功
                HttpServletRequest request = (HttpServletRequest) servletRequest;
                String username = (String) request.getAttribute("username");
                String identity = (String) request.getAttribute("identity");
                log.info("校内教师登陆成功" + username);
                String sign = jwtUtils.sign(acc);
                accountDTO.setName("欢迎老师："+teacher.getTeacherName());
                return new R(Code.LOGIN_OK, accountDTO, sign);
            }
        } else if (accountIdentity == 3) {
            //3.1校外
            LambdaQueryWrapper<OutTeacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OutTeacher::getTeacherPhone, accountUsername);
//            Teacher teacher = teacherDao.selectOne(queryWrapper);
//            OutTeacher outTeacher = outTeacherDao.selectOne(queryWrapper);
            OutTeacher outTeacher
                    = outTeacherService.getOne(queryWrapper);

            //3.2如果没有查询到则返回登录失败结果
            if (outTeacher == null) {
                return new R(Code.LOGIN_ERR, null, "没有该企业教师账户");
            }
            //3.3.密码比对，如果不一致则返回登录失败结果
            if (!outTeacher.getOutPassword().equals(accountPassword)
            ) {
                return new R(Code.LOGIN_ERR, null, "企业教师登录密码错误");
            } else {
                //3.4登陆成功
                HttpServletRequest request = (HttpServletRequest) servletRequest;
                String username = (String) request.getAttribute("username");
                String identity = (String) request.getAttribute("identity");
                log.info("企业教师登陆成功" + username);
                String sign = jwtUtils.sign(acc);
                accountDTO.setName("外聘："+outTeacher.getTeacherName());
                return new R(Code.LOGIN_OK, accountDTO, sign);
            }
        } else if (accountIdentity == 4) {
            //4.1管理员
            LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Admin::getAdminPhone, accountUsername);
//            Teacher teacher = teacherDao.selectOne(queryWrapper);
            Admin admin = adminDao.selectOne(queryWrapper);

            //4.2如果没有查询到则返回登录失败结果
            if (admin == null) {
                return new R(Code.LOGIN_ERR, null, "没有该管理员账户");
            }
            //4.3.密码比对，如果不一致则返回登录失败结果
            if (!admin.getAdminPassward().equals(accountPassword)
            ) {
                return new R(Code.LOGIN_ERR, null, "管理员登录密码错误");
            } else {
                //4.4登陆成功
                HttpServletRequest request = (HttpServletRequest) servletRequest;
                String username = (String) request.getAttribute("username");
                String identity = (String) request.getAttribute("identity");
                log.info("管理员登陆成功" + username);
                String sign = jwtUtils.sign(acc);
                accountDTO.setName("管理员");
                return new R(Code.LOGIN_OK, accountDTO, sign);
            }
        } else if (accountIdentity == 5) {
            //5.1 企业
            LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Company::getCompanyPhone, accountUsername);
//            Teacher teacher = teacherDao.selectOne(queryWrapper);
            Company company = companyDao.selectOne(queryWrapper);

            //5.2如果没有查询到则返回登录失败结果
            if (company == null) {
                return new R(Code.LOGIN_ERR, null, "没有该企业账户");
            }
            //5.3.密码比对，如果不一致则返回登录失败结果
            if (!company.getCompanyPassword().equals(accountPassword)
            ) {
                return new R(Code.LOGIN_ERR, null, "企业登录密码错误");
            } else {
                //6.4登陆成功
                HttpServletRequest request = (HttpServletRequest) servletRequest;
                String username = (String) request.getAttribute("username");
                String identity = (String) request.getAttribute("identity");
                log.info("企业登陆成功" + username);
                String sign = jwtUtils.sign(acc);
                accountDTO.setName("企业");
                return new R(Code.LOGIN_OK, accountDTO, sign);
            }
        }

        return new R(Code.LOGIN_ERR,null,"登录请求有问题");
    }
}